package com.xpmusic.model;

import java.util.HashMap;
import java.util.Map;

public class RegisterFormBean {
    private String name;
    private String password1;
    private String password2;
    private String email;
    private Map<String , String > errors = new HashMap<String , String >();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean validate (){
        boolean flag = true ;
        if(name == null || name.trim().equals("")){
            errors.put("name", "请输入姓名！");
            flag = false;
        }
        if(password1 == null || password1.trim().equals("")){
            errors.put("password" , "请输入密码！");
            flag = false;
        }else if(password1.length() > 12 || password1.length() < 6){
            errors.put("password", "请输入6-12位的密码！");
            flag = false;
        }
        if(password2 != null && !password2.equals(password1)){
            errors.put("password2","两次输入密码不一致!");
            flag = false;
        }
        //对Email判断格式
        if(email == null || email.trim().equals("")){
            errors.put("eamil" , "请输入邮箱！");
            flag = false;
        }else if(!email.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9]_-]+(\\\\.[a-zA-Z0-9_-]+)+")){
            errors.put("email" , "邮箱格式错误！");
            flag = false;
        }
        return flag ;
    }

    /**
     * 向Map中添加错误信息
     * @param err
     * @param errMsg
     */
    public void setErrorMsg(String err, String errMsg){
        if((err != null) && (errMsg != null)){
            errors.put(err, errMsg);
        }
    }

    /**
     * 获取errors集合
     * @return
     */
    public Map<String , String > getErrors(){
        return errors;
    }
}
